package hysoft.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Spring mvc的自定义配置类，比如cros跨域等。

 * @author
 * @date
 */
@Slf4j
@Configuration
public class WebMvcConfigure implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        /** todo: 自定义配置（强制） 微服务的默认首页，为swagger的服务接口页面 */
        registry.addViewController("/").setViewName("redirect:swagger-ui.html");
        /** todo: 自定义配置（可选） */
    }

    /**
     * 配置静态访问资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        /** todo: 自定义配置（可选） 跨域配置，默认不做限制 */
        //        registry.addMapping("/api/**")
        //                .allowedOrigins("http://domain2.com")
        //                .allowedMethods("PUT", "DELETE")
        //                .allowedHeaders("header1", "header2", "header3")
        //                .exposedHeaders("header1", "header2")
        //                .allowCredentials(false).maxAge(3600);
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("X-Suggested-Filename")
                .exposedHeaders("X-Suggested-Filename", "Content-Type")
                //.allowedOrigins("localhost:8080")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE");
    }
}
